package mipushdemo.xiaomi.com.diyitonghang.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import mipushdemo.xiaomi.com.diyitonghang.R;
import mipushdemo.xiaomi.com.diyitonghang.base.BaseFragment;
import mipushdemo.xiaomi.com.diyitonghang.dao.DaoMaster;
import mipushdemo.xiaomi.com.diyitonghang.dao.DaoSession;
import mipushdemo.xiaomi.com.diyitonghang.dao.TabBeanDao;
import mipushdemo.xiaomi.com.diyitonghang.dao.TabXBeanDao;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.NewsChannelData;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.TabBean;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.TabXBean;
import mipushdemo.xiaomi.com.diyitonghang.presenter.contract.homecontract.IHomeContract;
import mipushdemo.xiaomi.com.diyitonghang.presenter.impl.loginimpl.HomePresenterImpl;
import mipushdemo.xiaomi.com.diyitonghang.ui.adapter.FragmentsPagerAdapter;
import mipushdemo.xiaomi.com.diyitonghang.ui.fragment.home.AddTabActivity;
import mipushdemo.xiaomi.com.diyitonghang.ui.fragment.home.HomeSonFragment;
import mipushdemo.xiaomi.com.diyitonghang.ui.fragment.home.SearchDataActivity;

public class HomeFragment extends BaseFragment implements IHomeContract.IHomeView<IHomeContract.IHomePresenter>, View.OnClickListener {
    @BindView(R.id.homeTabLayout)
    TabLayout homeTabLayout;
    @BindView(R.id.homeViewPager)
    ViewPager homeViewPager;
    Unbinder unbinder;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    private IHomeContract.IHomePresenter presenter;
    private String header = "application/x-www-form-urlencoded";
    public static List<NewsChannelData.DataBean.NewsChannelListBean> mList = new ArrayList();
    public static List<String> mTitle = new ArrayList<>();
    public static List<String> mXTitle = new ArrayList<>();
    public static List<String> mSearchList = new ArrayList<>();
    public static List<HomeSonFragment> mFragment = new ArrayList<>();
    private FragmentsPagerAdapter adapter;
    private TabBeanDao tabBeanDao;
    private TabXBeanDao tabXBeanDao;

    @Override
    protected void init(View view) {
        presenter = new HomePresenterImpl(this);
        presenter.login(header);
        ivAdd.setOnClickListener(this);
        ivSearch.setOnClickListener(this);
        DaoSession tab = DaoMaster.newDevSession(getContext(), "tab");
        tabBeanDao = tab.getTabBeanDao();
        tabXBeanDao = tab.getTabXBeanDao();

    }

    @Override
    protected void loadData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void showData(List<NewsChannelData.DataBean.NewsChannelListBean> newsChannelList) {
        if (tabBeanDao.loadAll().size() == 0) {
            mList.addAll(newsChannelList);
            for (int i = 0; i < mList.size(); i++) {
                NewsChannelData.DataBean.NewsChannelListBean newsChannelListBean = mList.get(i);
                mSearchList.add(newsChannelListBean.getChannelName());
                if (i < 12) {
                    tabBeanDao.insert(new TabBean(newsChannelListBean.getChannelId(), newsChannelListBean.getChannelName()));
                } else {
                    tabXBeanDao.insert(new TabXBean(newsChannelListBean.getChannelId(), newsChannelListBean.getChannelName()));
                }
            }
        }
        List<TabBean> tabBeans = tabBeanDao.loadAll();
        for (TabBean tabBean : tabBeans) {
            mTitle.add(tabBean.getChannelName());
            mFragment.add(addfagment(tabBean));
        }
//        for (int i = 0; i < 12; i++) {
//            mTitle.add(mList.get(i).getChannelName());
//            mFragment.add(addfagment(mList.get(i)));
//        }
//        for (int i = 12; i < mList.size(); i++) {
//            mXTitle.add(mList.get(i).getChannelName());
//        }


        adapter = new FragmentsPagerAdapter(getChildFragmentManager(), mFragment, mTitle);
        homeTabLayout.setupWithViewPager(homeViewPager);
        homeViewPager.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void setPresenter(IHomeContract.IHomePresenter iHomePresenter) {
        presenter = iHomePresenter;
    }

    public static HomeSonFragment addfagment(TabBean bean) {

        HomeSonFragment kindsSonFragment = new HomeSonFragment();
        Bundle args = new Bundle();
        args.putSerializable("bean", bean);
        kindsSonFragment.setArguments(args);
        return kindsSonFragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_add:
                startActivityForResult(new Intent(getContext(), AddTabActivity.class), 0);
                break;
            case R.id.iv_search:
                startActivity(new Intent(getContext(),SearchDataActivity.class));
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == 1) {
            mTitle.clear();
            mFragment.clear();
            List<TabBean> tabBeans = tabBeanDao.loadAll();
            for (TabBean tabBean : tabBeans) {
                mTitle.add(tabBean.getChannelName());
                mFragment.add(addfagment(tabBean));
            }
            adapter.notifyDataSetChanged();
        }
    }
    //    @Override
//    public void onResume() {
//        super.onResume();
//        adapter.notifyDataSetChanged();
//    }
}
